package com.ruoyi.park.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.park.domain.BuiPark;
import com.ruoyi.park.domain.vo.BuildingInfoVO;
import com.ruoyi.park.domain.vo.ParkInfoVO;
import com.ruoyi.park.mapper.BuiBuildingMapper;
import com.ruoyi.park.mapper.BuiParkMapper;
import com.ruoyi.park.service.IBuiGreenAreaService;
import com.ruoyi.park.service.IBuiParkService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 园区Service业务层处理
 *
 * @author cjie
 * @date 2021-07-20
 */
@Service
@RequiredArgsConstructor
public class BuiParkServiceImpl extends LinkFileServiceImpl implements IBuiParkService {
    private final BuiParkMapper buiParkMapper;
    private final BuiBuildingMapper buildingMapper;

    /**
     * 查询园区
     *
     * @param id 园区ID
     * @return 园区
     */
    @Override
    public BuiPark selectBuiParkById(String id) {
        BuiPark buiPark = buiParkMapper.selectBuiParkById(id);
        buiPark.setFileList(getFileList(id));
        return buiPark;
    }

    /**
     * 查询园区列表
     *
     * @param buiPark 园区
     * @return 园区
     */
    @Override
    public List<BuiPark> selectBuiParkList(BuiPark buiPark) {
        return buiParkMapper.selectBuiParkList(buiPark);
    }

    @Override
    public List<BuiPark> selectBuiParkByCode(String code) {
        BuiPark buiPark = new BuiPark();
        buiPark.setParkNum(code);
        return buiParkMapper.selectBuiParkList(buiPark);
    }

    /**
     * 新增园区
     *
     * @param buiPark 园区
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertBuiPark(BuiPark buiPark) {
        buiPark.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        buiPark.setCreateTime(DateUtils.getNowDate());
        buiPark.setCreateBy(SecurityUtils.getUsername());


        if(UserConstants.NOT_UNIQUE.equals(checkParkNumUnique(buiPark))){
            return AjaxResult.error("当前园区编号已存在");
        }

        linkFile(buiPark.getFileIdList(), buiPark.getId());
        return buiParkMapper.insertBuiPark(buiPark) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 校验园区编号是否唯一
     * @param buiPark
     * @return
     */
    private String checkParkNumUnique(BuiPark buiPark) {
        BuiPark source = buiParkMapper.selectBuiParkByParkNum(buiPark.getParkNum());
        if(source != null && !source.getId().equals(buiPark.getId())){
            return UserConstants.NOT_UNIQUE;
        }

        return UserConstants.UNIQUE;
    }

    /**
     * 修改园区
     *
     * @param buiPark 园区
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateBuiPark(BuiPark buiPark) {
        buiPark.setUpdateTime(DateUtils.getNowDate());
        buiPark.setUpdateBy(SecurityUtils.getUsername());

        if(UserConstants.NOT_UNIQUE.equals(checkParkNumUnique(buiPark))){
            return AjaxResult.error("当前园区编号已存在");
        }

        linkFile(buiPark.getFileIdList(), buiPark.getId());
        return buiParkMapper.updateBuiPark(buiPark) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 批量删除园区
     *
     * @param ids 需要删除的园区ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBuiParkByIds(String[] ids) {
        deleteLinkFile(ids);
        return buiParkMapper.deleteBuiParkByIds(ids);
    }

    /**
     * 删除园区信息
     *
     * @param id 园区ID
     * @return 结果
     */
    @Override
    public int deleteBuiParkById(String id) {
        return buiParkMapper.deleteBuiParkById(id);
    }

    /**
     * 查询园区楼栋树
     *
     * @return
     */
    @Override
    public List<ParkInfoVO> selectParkTree() {
      List<ParkInfoVO> parkInfoVOList = buiParkMapper.selectIdAndNumList();
      parkInfoVOList.forEach(parkInfoVO -> {
       List<BuildingInfoVO> buildingInfoVOList = buildingMapper.selectIdAndNumByParkId(parkInfoVO.getId());
          parkInfoVO.setBuildingInfoVOList(buildingInfoVOList);
      });
      return parkInfoVOList;
    }
}
